﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLCONGNOLIBS.Controllers;
using QLCONGNOLIBS.Models;
using System.IO;


namespace QLCONGNOAPPs.UserForms.ImageManagers
{
    public partial class frmBrowserImages : Form
    {
        ImageEn m_objimgEntity;
        public frmBrowserImages(ImageEn objimgEntity)
        {
            InitializeComponent();
            m_objimgEntity = objimgEntity;
            if(m_objimgEntity.choosed){
                picImage.Image = (new ImageControllers()).ConvertByteToImage(objimgEntity.byImage);
            }
        }

        private void btnChooseImg_Click(object sender, EventArgs e)
        {            
            try
            {
                this.Cursor = Cursors.WaitCursor;
                OpenFileDialog objDialog = new OpenFileDialog();
                objDialog.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*";
                objDialog.Title = "Chọn Ảnh";
                if (objDialog.ShowDialog() == DialogResult.OK)
                {
                    string strPath = objDialog.FileName.ToString();
                    txtImages.Text = strPath;
                    byte[] byImages = (new ImageControllers()).ConvertImageToByte(strPath);
                    picImage.Image = (new ImageControllers()).ConvertByteToImage(byImages);

                    m_objimgEntity.imgId = 1;
                    m_objimgEntity.imgPath = strPath;
                    m_objimgEntity.imgName = Path.GetFileName(strPath);
                    m_objimgEntity.byImage = byImages;
                    m_objimgEntity.choosed = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally {
                this.Cursor = Cursors.Default;
            }
        }       
    }
}
